package com.example.tliaswebmanagement.service.impl;

import com.example.tliaswebmanagement.entity.*;
import com.example.tliaswebmanagement.mapper.EmpExprMapper;
import com.example.tliaswebmanagement.mapper.EmpMapper;
import com.example.tliaswebmanagement.service.EmpLogService;
import com.example.tliaswebmanagement.service.EmpService;
import com.example.tliaswebmanagement.util.JwtUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    private final EmpMapper empMapper;
    private final EmpExprMapper empExprMapper;
    private final EmpLogService empLogService;

    @Autowired
    public EmpServiceImpl(EmpMapper empMapper, EmpExprMapper empExprMapper, EmpLogService empLogService){
        this.empMapper = empMapper;
        this.empExprMapper = empExprMapper;
        this.empLogService = empLogService;
    }
    //1.不采用PageHelper插件开发
    /*@Override
    public List<Emp> findAll(Integer page,Integer pageSize){

        long total = empMapper.count();
        Integer start=(page-1)*pageSize;
        List<Emp> emps=empMapper.findAll(start,pageSize);
        return new PageResult(total,emps);
    }*/
    //2.采用PageHelper插件开发
    @Override
    public PageResult findAll(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page,pageSize);
        //执行查询
        List<Emp> emps=empMapper.findAll(name,gender,begin,end);
        //封装到Page里面
        Page<Emp> p=(Page<Emp>) emps;
        //封装结果
        return new PageResult(p.getTotal(), p.getResult());
    }

    //插入新建员工信息
    //使用spring框架的事务管理，确保数据一致性，
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void insert(Emp emp){
        try{//设置未传输数据
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            //插入基本信息
            empMapper.insert(emp);

            //保存工作经历信息
            //1.先获取新增员工id
            Integer id = emp.getId();
            //2.打包获取工作经历信息
            List<EmpExpr> exprlist = emp.getEmprList();
            //3.先判空再插入
            if (!CollectionUtils.isEmpty(exprlist)) {
                //将每条工作经历的员工id设为刚插入的员工id信息
                exprlist.forEach(Empexpr -> Empexpr.setEmpId(id));
                //插入员工工作经历到empexpr表
                empExprMapper.insertExpr(exprlist);
            }
        }finally {
            //记录操作日志
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), emp.toString());
            empLogService.insertLog(empLog);
        }

    }

    //删除员工
    @Transactional
    @Override
    public void deleteById(List<Integer> ids) {
        empMapper.deleteById(ids);
        empExprMapper.deleteByEmpId(ids);
    }

    //修改员工,数据回显
    @Override
    public Emp getInfoById(Integer id) {
        return empMapper.getInfoById(id);
    }
    //更新数据
    @Transactional
    @Override
    public void update(Emp emp){
        //更新员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
        //更新工作经历，先删除旧的，在插入新的经历
        empExprMapper.deleteByEmpId(Arrays.asList(emp.getId()));
        //插入新的
        Integer id=emp.getId();
        List<EmpExpr> emprList=emp.getEmprList();
        if (!CollectionUtils.isEmpty(emprList)){
            emprList.forEach(Empexpr->Empexpr.setEmpId(id));
            empExprMapper.insertExpr(emprList);
        }
    }

    //获取员工全部信息
    @Override
    public List<Emp> getAll() {
        return empMapper.getAll();
    }

    //获取员工用户名和密码
    @Override
    public LoginInfo login(Emp emp) {
        Emp emp1= empMapper.getEmpByUsernameAndPassword(emp);
        if (emp1!=null){
            //生成jwt令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", emp1.getId());
            claims.put("username", emp1.getUsername());

            String jwt= JwtUtils.generateJwtToken(claims);



            LoginInfo loginInfo=new LoginInfo(emp1.getId(),emp1.getUsername(),emp1.getName(),jwt);
            return loginInfo;
        }
        return null;

    }
}
